#include "include/blk.h"
#include "include/hvc.h"
#include "include/irq.h"
#include "include/shyper_service.h"

int mediated_dev_num = 0;
struct mediated_cache mediated_cache_list[VM_NUM_MAX];

irqreturn_t irq_handler(int irq, void *handle) {
    bool ret = false;
    struct hvc_irq_arg *irq_arg;
    shyper_dev.cfg_ptr += VM_PAGE_SIZE / VM_NUM_MAX;
    if (shyper_dev.cfg_ptr - shyper_dev.cfg_queue >= VM_PAGE_SIZE) {
        shyper_dev.cfg_ptr = shyper_dev.cfg_queue;
    }
    irq_arg = (struct hvc_irq_arg *)shyper_dev.cfg_ptr;

    // INFO("irq type: 0x%08x event: %d\n", (u32)irq_arg->fid,
    //        (u32)irq_arg->event);

    switch (irq_arg->fid) {
    case HVC_SYS:
        ret = hvc_sys_handler(irq_arg);
        break;
    case HVC_VMM:
        ret = hvc_vmm_handler(irq_arg);
        break;
    case HVC_IVC:
        ret = hvc_ivc_handler(irq_arg);
        break;
    case HVC_MEDIATED:
        ret = hvc_mediated_handler(irq_arg);
        break;
    case HVC_SECURITY:
        ret = hvc_security_handler(irq_arg);
        break;
    case HVC_CONFIG:
        ret = hvc_config_handler(irq_arg);
        break;
    case HVC_UNILIB:
        ret = hvc_unilib_handler(irq_arg);
        break;
    case HVC_SHMEM:
        ret = hvc_shmem_handler(irq_arg);
        break;
    default:
        ret = false;
        break;
    }
    if (!ret) {
        WARNING("%s: cannot match fid 0x%08x and event 0x%08x\n", __func__,
                (u32)irq_arg->fid, (u32)irq_arg->event);
    }
    return IRQ_HANDLED;
}